package com.hxzy.controller;

import com.hxzy.common.annon.RequiredMerchant;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.dto.ChangeStatusDTO;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.common.vo.R;
import com.hxzy.dto.ProductDishFlavorDTO;
import com.hxzy.dto.ProductSearchDTO;
import com.hxzy.service.ProductService;
import com.hxzy.vo.ProductDishFlavorVO;
import com.hxzy.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 商品信息
 * @author Administrator
 */
@Api(tags = "商品信息API")
@RestController
@RequestMapping(value = "/api/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @ApiOperation(value = "商品分页查询")
    @GetMapping(value = "/data")
    public R search(ProductSearchDTO productSearchDTO){

        //如果是就只允许查询自己的商品信息
        if(!WaimaiThreadLocal.isSuperMan()){
            Long id=WaimaiThreadLocal.getCurrent().getAccountVO().getId();
            productSearchDTO.setMerchantId(id);
        }

        super.startPageHelper();
        List<ProductVO> arr=this.productService.search(productSearchDTO);
        return super.pageToPageVO(arr);
    }

    @ApiOperation(value = "新增")
    @RequiredMerchant
    @PostMapping
    public R add(@RequestBody @Valid ProductDishFlavorDTO  productDishFlavorDTO){

        boolean result=this.productService.add(productDishFlavorDTO);

        return super.toR(result);
    }

    @ApiOperation(value = "修改")
    @RequiredMerchant
    @PutMapping
    public R update(@RequestBody @Valid ProductDishFlavorDTO  productDishFlavorDTO){
        boolean result=this.productService.edit(productDishFlavorDTO);
        return super.toR(result);
    }

    @RequiredMerchant
    @ApiOperation(value = "更改状态")
    @PostMapping(value = "/change/status")
    public R changeStatus(@RequestBody @Valid ChangeStatusDTO changeStatusDTO){
        boolean result=this.productService.changeStatus(changeStatusDTO);
        return super.toR(result);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R findById(@PathVariable(value = "id") Long id){
        ProductDishFlavorVO productDishFlavorVO= this.productService.findById(id);
        return R.okHasData(productDishFlavorVO);
    }
}
